package com.maon.mall.security.controller;


import com.mano.commons.web.base.BaseWebController;
import com.mano.commons.web.constants.WebConstants;
import com.mano.mall.api.dto.member.input.UserLoginInpDTO;
import com.mano.mall.api.dto.member.output.UserOutputDTO;
import com.mano.mall.common.base.BaseResponse;
import com.maon.mall.security.feign.MemberServiceFeign;
import com.maon.mall.security.model.SsoUser;
import com.maon.mall.security.utils.SsoWebLoginHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.UUID;

/**
 * sso server (for web)
 *
 * @author xuxueli 2017-08-01 21:39:47
 */
@Controller
public class WebController extends BaseWebController {

    @Autowired
    private MemberServiceFeign memberServiceFeign;


    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {

        // login check
        // 验证 access_token
        SsoUser ssoUser = SsoWebLoginHelper.loginCheck(request, response);

        if (ssoUser == null) {
            return "redirect:/login";
        } else {
            model.addAttribute("xxlUser", ssoUser);
            return "index";
        }
    }

    /**
     * Login page
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(WebConstants.SSO_LOGIN)
    public String login(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {

        // login check
        SsoUser xxlUser = SsoWebLoginHelper.loginCheck(request, response);

        if (xxlUser != null) {

            // success redirect
            String redirectUrl = request.getParameter(WebConstants.REDIRECT_URL);
            if (redirectUrl!=null && redirectUrl.trim().length()>0) {

                return "redirect:" + redirectUrl;
            } else {
                return "redirect:/";
            }
        }

        model.addAttribute("errorMsg", request.getParameter("errorMsg"));
        model.addAttribute(WebConstants.REDIRECT_URL, request.getParameter(WebConstants.REDIRECT_URL));
        return "login";
    }

    /**
     * Login
     *
     * @param request
     * @param redirectAttributes
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/doLogin")
    public String doLogin(HttpServletRequest request,
                        HttpServletResponse response,
                        RedirectAttributes redirectAttributes,
                        String username,
                        String password,
                        String ifRemember) {

        boolean ifRem = (ifRemember!=null&&"on".equals(ifRemember))?true:false;

        // valid login  调用会员服务进行验证
//        ReturnT<UserInfo> result = userService.findUser(username, password);
//        if (result.getCode() != ReturnT.SUCCESS_CODE) {
//            redirectAttributes.addAttribute("errorMsg", result.getMsg());
//
//            redirectAttributes.addAttribute(Conf.REDIRECT_URL, request.getParameter(Conf.REDIRECT_URL));
//            return "redirect:/login";
//        }
        UserLoginInpDTO userLoginInpDTO = new UserLoginInpDTO();
        userLoginInpDTO.setMobile(username);
        userLoginInpDTO.setPassword(password);
        userLoginInpDTO.setLoginType(WebConstants.MEMBER_LOGIN_TYPE_PC);
        String info = webBrowserInfo(request);
        userLoginInpDTO.setDeviceInfor(info);

        BaseResponse<UserOutputDTO> ssoLogin  = memberServiceFeign.ssoLogin(userLoginInpDTO);
        if (!isSuccess(ssoLogin)) {
            redirectAttributes.addAttribute("errorMsg", ssoLogin.getMsg());
            redirectAttributes.addAttribute(WebConstants.REDIRECT_URL, request.getParameter(WebConstants.REDIRECT_URL));
            return "redirect:/login";
        }

        UserOutputDTO data = ssoLogin.getData();
        if (data == null) {
            redirectAttributes.addAttribute("errorMsg", "用户信息数据为空!");
            redirectAttributes.addAttribute(WebConstants.REDIRECT_URL, request.getParameter(WebConstants.REDIRECT_URL));
            return "redirect:/login";
        }

        // 4、return, redirect sessionId
        String redirectUrl = request.getParameter(WebConstants.REDIRECT_URL);
        if (redirectUrl!=null && redirectUrl.trim().length()>0) {
            return "redirect:" + redirectUrl;
        } else {
            return "redirect:/";
        }

    }

    /**
     * Logout
     *
     * @param request
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(WebConstants.SSO_LOGOUT)
    public String logout(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {

        // logout

        redirectAttributes.addAttribute(WebConstants.REDIRECT_URL, request.getParameter(WebConstants.REDIRECT_URL));
        return "redirect:/login";
    }


}